package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.MD5Builder;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class LoginActivity extends AppCompatActivity {
    private boolean flag = true;
    private ImageView hidepassword;
    private EditText et_activity_login_password;
    private TextView mRegister;
    private AutoRelativeLayout autoRelativeLayout;
    private TextView mForgetPass;
    private ImageView logincal;
    private Button mjump;
    private Button btn_activity_login_login;
    private EditText et_activity_login_user;
    private String userName;
    private String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_login);
        initView();//初始化控件
        setOnClick();//点击事件
    }
    private void initView() {
        et_activity_login_password = (EditText) findViewById(R.id.et_activity_login_password);
        et_activity_login_user = (EditText) findViewById(R.id.et_activity_login_user);
        hidepassword = (ImageView) findViewById(R.id.hidepassword);
        mRegister = (TextView) findViewById(R.id.mRegister);//注册
        mForgetPass = (TextView) findViewById(R.id.mForgetPass);
        logincal = (ImageView) findViewById(R.id.logincal);
        btn_activity_login_login = (Button) findViewById(R.id.btn_activity_login_login);
    }
    //设置点击事件
    public void setOnClick() {
        //设置点击密码展示隐藏
        hidepassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    et_activity_login_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    hidepassword.setImageResource(R.drawable.kejianloginpassword);
                    flag = false;
                } else {
                    et_activity_login_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    hidepassword.setImageResource(R.drawable.bukejianloginpassword);
                    flag = true;
                }
            }
        });
        //点击取消 finish掉当前页面
        logincal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
               /* Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);*/
                overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
            }
        });
        //注册
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        //忘记密码
        mForgetPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ForgetPassWordActivity.class);
                startActivity(intent);
            }
        });
        //登录
        btn_activity_login_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwd = et_activity_login_password.getText().toString().trim();
                userName = et_activity_login_user.getText().toString().trim();
                loginMethod(userName, pwd);
            }
        });
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
    //登录的方法
    public void loginMethod(final String userName, final String pwd) {
        MD5Builder md5Builder=new MD5Builder();
        final String password = md5Builder.getMD5Str(pwd);
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userName", userName);
        params.put("pwd", password);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.loginUrl, params, new CallBackListener() {
            @Override
            public void onStart() {}
            @Override
            public void onSuccess(Object result) {
                Log.e("zzz",""+result);
                String string = (String) result;
                Gson gson = new Gson();
                LoginHuancunBean loginHuancunBean1 = gson.fromJson(string, LoginHuancunBean.class);
                String state = loginHuancunBean1.result.state;
                ToastUtils.showShort(LoginActivity.this,""+loginHuancunBean1.result.message);
                if (state.equals("1")) {
                    ACache.get(LoginActivity.this).put("userName",userName, 30 * 24 * 60 * 60 * 60);
                    ACache.get(LoginActivity.this).put("pwd",password, 30 * 24 * 60 * 60 * 60);
                    ACache.get(LoginActivity.this).put("loginId",string, 30 * 24 * 60 * 60 * 60);
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                    //ACache.get(LoginActivity.this).put("login",loginHuancunBean1,3 * 24 * 60 * 60 * 60);
                    //查询
                    //loginBean = (LoginBean) ACache.get(LoginActivity.this).getAsObject("A");
                    //清除
                    //ACache.get(LoginActivity.this).clear();
                }
                ToastUtils.showShort(LoginActivity.this,""+loginHuancunBean1.result.message);
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
}
